﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Npgsql;
using System.IO;
using test2;

namespace Aplikacja_PostgreSQL
{
    public partial class MainWindow : Form, IDisposable
    {
        private About info;
        private Settings windowSettings;
        private NpgsqlConnection conn;
        private NpgsqlDataAdapter da;
        private NpgsqlCommand command;
        private Query query = null;
        private DataSet ds = new DataSet();
        private DataTable dt = new DataTable();

        static string dbDatabase;

        public static string DbDatabase
        {
            get { return dbDatabase; }
            set { dbDatabase = value; }
        }
        static string dbUserID;

        public static string DbUserID
        {
            get { return dbUserID; }
            set { dbUserID = value; }
        }
        static string dbPassword;

        public static string DbPassword
        {
            get { return dbPassword; }
            set { dbPassword = value; }
        }
        static string dbPort;

        public static string DbPort
        {
            get { return dbPort; }
            set { dbPort = value; }
        }
        static string dbSerwer;

        public static string DbSerwer
        {
            get { return dbSerwer; }
            set { dbSerwer = value; }
        }

        public MainWindow()
        {
            InitializeComponent();
            Files.OpenFile();
        }

        private void btClear_Click(object sender, EventArgs e)
        {
            tbCommandBox.Clear();
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            tbCommandLog.Clear();
        }

        private void connectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (!(Conn.IsConnected(ref conn)))
                    Conn.Connect(ref conn, DbSerwer, DbDatabase, DbUserID, DbPassword, DbPort);
                else
                    MessageBox.Show("Already connected");
                if (Conn.IsConnected(ref conn))
                {
                    tbCommandLog.Text =  DataCzas.GetTime() + String.Format(" Connected to {0}", conn.Database);
                    lbStatus.Text = "Connected";
                }
            }
            catch(Exception excp)
            {
                MessageBox.Show(excp.Message.ToString());
            }
        }

        void IDisposable.Dispose()
        {
            Conn.Disconnect(ref conn);
        }

        private void disconnectToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (Conn.Disconnect(ref conn))
            {
                tbCommandLog.Text = DataCzas.GetTime() + String.Format(" Disconnected from {0}", dbDatabase);
                lbStatus.Text = "Disconnected";
            }

        }

        private void btExecute_Click(object sender, EventArgs e)
        {
            if (Conn.IsConnected(ref conn))
            {
                if (query == null)
                    query = new Query(tbCommandBox.Text.ToString());
                else
                    query.SetQuery(tbCommandBox.Text.ToString());
                if (query.QueryAnalyze() == 1)
                {
                    try
                    {
                        da = new NpgsqlDataAdapter(query.GetQuery(), conn);
                        ds.Reset();
                        da.Fill(ds);
                        dt = ds.Tables[0];
                        dataOutput.DataSource = dt;
                        lbQuery.Text = query.GetQuery();
                        da = null;
                        tbCommandLog.Text += Environment.NewLine + DataCzas.GetTime() + "Execute OK";
                    }
                    catch (Exception excp)
                    {
                        tbCommandLog.Text += Environment.NewLine + DataCzas.GetTime() + excp.Message.ToString();
                    }
                }
                else if(query.QueryAnalyze() == 2)
                {
                    try
                    {
                        command = new NpgsqlCommand(query.GetQuery(), conn);
                        int count = command.ExecuteNonQuery();
                        if (count == 1)
                            tbCommandLog.Text += Environment.NewLine + DataCzas.GetTime() + count.ToString() + " row inserted";
                        else
                            tbCommandLog.Text += Environment.NewLine + DataCzas.GetTime() + count.ToString() + " rows inserted";
                        command = null;
                    }
                    catch (Exception excp)
                    {
                        tbCommandLog.Text += Environment.NewLine + DataCzas.GetTime() + excp.Message.ToString();
                    } 
                }
                else if (query.QueryAnalyze() == 3)
                {
                    try
                    {
                        command = new NpgsqlCommand(query.GetQuery(), conn);
                        int count = (int)command.ExecuteNonQuery();
                        if (count == 1)
                            tbCommandLog.Text += Environment.NewLine + DataCzas.GetTime() + count.ToString() + " row updated";
                        else
                            tbCommandLog.Text += Environment.NewLine + DataCzas.GetTime() + count.ToString() + " rows updated";
                        command = null;
                    }
                    catch (Exception excp)
                    {
                        tbCommandLog.Text += Environment.NewLine + DataCzas.GetTime() + excp.Message.ToString();
                    } 
                }
                else if (query.QueryAnalyze() == 4)
                {
                    try
                    {
                        command = new NpgsqlCommand(query.GetQuery(), conn);
                        int count = command.ExecuteNonQuery();
                        if (count == 1)
                            tbCommandLog.Text += Environment.NewLine + DataCzas.GetTime() + count.ToString() + " row deleted";
                        else
                            tbCommandLog.Text += Environment.NewLine + DataCzas.GetTime() + count.ToString() + " rows deleted";
                        command = null;
                    }
                    catch (Exception excp)
                    {
                        tbCommandLog.Text += Environment.NewLine + DataCzas.GetTime() + excp.Message.ToString();
                    } 
                }
                else
                {
                    tbCommandLog.Text = DataCzas.GetTime() + "Unknown command";
                }
            }
            else
            {
                tbCommandLog.Text = DataCzas.GetTime() + "Not Connected";
            }
        }

        private void ustawieniaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (windowSettings == null || windowSettings.IsDisposed)
            {
                windowSettings = new Settings();
                windowSettings.Accept += new Settings.AppSettingsEventHandler(windowSettings_Accept);
            }
            windowSettings.Show();
        }

        void windowSettings_Accept(object sender, Settings.AppSettingsEventArgs e)
        {
            DbDatabase = e.Database;
            DbSerwer = e.Serwer;
            DbPort = e.Port;
            DbPassword = e.Password;
            DbUserID = e.Userid;
            Files.ReWriteFile();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            info = new About();
            info.Show();
        }

        private void btExecute_MouseEnter(object sender, EventArgs e)
        {
            ttBtExecute.Show("Execute", btExecute, 1000) ;
        }

        private void btClear_MouseEnter(object sender, EventArgs e)
        {
            ttBtExecute.Show("Clear command", btClear, 1000);
        }

        private void lbStatus_MouseEnter(object sender, EventArgs e)
        {
            
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }
    }
}
